package com.mtsmda.test.comanda.d;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DirectoryTree {
	public static final String START = "start";
	public static final String END = "end";
	public static final String TAB = "\t";
	public static final String ENTER = "\n";

	private static int countMargin = 1;
	private static int countFile = 0;
	private static int countDirectory = 0;
	private static int countHidden = 0;

	public static void main(String[] args) {
		StringBuilder builder = new StringBuilder("");
		File root = getAllLocalDisks()[2];
		getFileTree(root, builder);
		System.out.println(ENTER + ENTER + "BUILDER");
		System.out.println(builder);
		writeToFile(builder, "info.txt");
	}

	public static File[] getAllLocalDisks() {
		File[] roots = File.listRoots();
		System.out.println("System Local Disks: ");
		for (int i = 0; i < roots.length; i++)
			System.out.println("Root[" + i + "]:" + roots[i]);
		return roots;
	}

	private static void getFileTree(String root, StringBuilder builder) {
		long beginTime = statistic();
		builder.append(ENTER).append("----- DISK - " + root + "-----")
				.append(ENTER);
		treeFiles(new File(root), "", builder);
		long endTime = statistic();
		if (builder.length() == 0) {
			builder.append(ENTER).append("***** DISK IS EMPTY *****")
					.append(ENTER);
		}

		builder.append(countStatistic(beginTime, endTime));
	}

	public static void getFileTree(File root, StringBuilder builder) {
		long beginTime = statistic();
		builder.append(ENTER)
				.append("----- DISK - " + root.getAbsolutePath() + "-----")
				.append(ENTER);
		treeFiles(root, "", builder);
		long endTime = statistic();

		if (builder.length() == 0) {
			builder.append(ENTER).append("***** DISK IS EMPTY *****")
					.append(ENTER);
		}

		builder.append(countStatistic(beginTime, endTime));
	}

	private static long statistic() {
		return System.currentTimeMillis();
	}

	private static StringBuilder countStatistic(long begin, long end) {
		StringBuilder builder = new StringBuilder(ENTER + ENTER);
		builder.append("*****Statistics*****")
				.append(ENTER)
				.append("begin - " + getNormalDateKind(begin, false, true))
				.append(ENTER)
				.append("end   - " + getNormalDateKind(end, false, true))
				.append(ENTER)
				.append("Time spent - ")
				.append((end - begin))
				.append(" milliseconds")
				.append((end - begin) > 1000 ? " or " + (double)(((end - begin) / 1000))
						+ " seconds" : "")
				.append(ENTER)
				.append(countDirectory + " directory(ies) is found!")
				.append(ENTER)
				.append(ENTER)
				.append(countFile + " file(s) is found!")
				.append(ENTER)
				.append(ENTER)
				.append(countHidden + " hidden file(s) or directory(ies) is found!")
				.append(ENTER)
				.append("OS - " + System.getProperty("os.name"))
				.append(ENTER)
				.append("OS version - " + System.getProperty("os.version"))
				.append(ENTER)
				.append("User name - " + System.getProperty("user.name"))
				.append(ENTER)
				.append("User language - "
						+ System.getProperty("user.language")).append(ENTER)
				.append("*****END*****");
		return builder;
	}

	public static String getNormalDateKind(long dateMS, boolean withoutSpace,
			boolean withHours) {
		String date = null;
		String pattern = "dd.MM.yyyy HH:mm:ss:S";
		if (withoutSpace) {
			pattern = "dd.MM.yyyy_HH:mm:ss:S";
		}

		if (!withHours) {
			pattern = pattern.substring(0, pattern.indexOf("H"));
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		if (dateMS == 0) {
			date = dateFormat.format(new Date());
		} else {
			date = dateFormat.format(new Date(dateMS));
		}
		return date;
	}

	private static void treeFiles(File file, String margin,
			StringBuilder builder) {

		if (file != null && file.exists() && file.isDirectory()) {
			builder.append(margin + file.getAbsolutePath() + ENTER);
			System.out.println(margin + file.getAbsolutePath());
			File[] listFiles = file.listFiles();
			try {
				for (File file2 : listFiles) {
					if (file2.isFile()) {
						countFile++;
					}
					
					if(file2.isDirectory()){
						countDirectory++;
					}
					
					if(file2.isHidden()){
						countHidden++;
					}
					treeFiles(file2, margin + margin(), builder);
				}
			} catch (Exception e) {
				return;
			}
		}
	}

	private static String margin() {
		String res = "";
		for (int i = 0; i < countMargin; i++) {
			res += TAB;
		}
		return res;
	}

	public static File writeToFile(StringBuilder builder, String fileName) {
		File file = null;
		try {
			file = new File(System.getProperty("user.home") + File.separator
					+ fileName);
			if (file != null) {
				if (!file.exists()) {
					file.createNewFile();
				}
				Writer writer = new FileWriter(file);
				writer.write(builder.toString());
				writer.flush();
				writer.close();
			}
		} catch (Exception e) {
			return file;
		}
		return file;
	}

}